const {mockTableList}=require('../model/table')
const { sendResponse } = require('../middleware')

const pageDataList=(data,currentPage,size)=>{
    return {total:mockTableList.total,list:data.slice(size*(currentPage-1),size*currentPage)}
}

 const getTableData = (req,res) => {
     let currentPage = Number(req.query.currentPage);
     let size= Number(req.query.size);
     sendResponse(res,0,'success',pageDataList(mockTableList.list,currentPage,size));
};
 const updateTableData = (req,res) => {
     mockTableList.list=mockTableList.list.map(item=>{
         if (item.id===req.body.id){
             item = req.body
         }
         return item
     })

     sendResponse(res,0,'success',mockTableList)
};
 const deleteTableDataById = (req,res) => {
     mockTableList.list=mockTableList.list.filter(item=>item.id!==Number(req.params.id));
     mockTableList.total--
     sendResponse(res,0,'success','删除成功')

};
const addTableData=(req,res)=>{
    mockTableList.list.unshift({...req.body,roles:'editor',phone:'16645409938','email':'331126135@qq.cin','createTime':'2015-04-14 01:12:14'})
    sendResponse(res,0,'success',mockTableList)

}
module.exports={
    addTableData,deleteTableDataById,updateTableData,getTableData
}
